package 牛客网_N.查找_递归_排序_贪心.下一个较大元素2;

import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int n = Integer.parseInt(in.nextLine());
		String str = in.nextLine();
		String[] dataStr = str.split(" ");
		int[] A = new int[dataStr.length];
		for(int i = 0; i < dataStr.length; i++){
			A[i] = Integer.parseInt(dataStr[i]);
		}
		System.out.println(findNext(A, n));
	}

	public static int[] findNext(int[] A, int n) {
		int[] dp = new int[A.length];
		for(int i = 0; i < A.length; i++){
			int minMax = -1;
			for(int j = i+1; j < A.length; j++){
                if(A[i] < A[j] && minMax == -1) minMax = A[j];
				if(A[i] < A[j] && A[j] < minMax) {
					minMax = A[j];
				}
			}
            dp[i] = minMax;
		}
		return dp;
	}
}
